package com.xz.knowledgeDuplication.pattern.framework.beans.factory.xml;

import com.xz.knowledgeDuplication.pattern.framework.beans.BeanDefinition;
import com.xz.knowledgeDuplication.pattern.framework.beans.MutablePropertyValues;
import com.xz.knowledgeDuplication.pattern.framework.beans.PropertyValue;
import com.xz.knowledgeDuplication.pattern.framework.beans.factory.support.BeanDefinitionReader;
import com.xz.knowledgeDuplication.pattern.framework.beans.factory.support.BeanDefinitionRegistry;
import com.xz.knowledgeDuplication.pattern.framework.beans.factory.support.SimpleBeanDefinitionRegistry;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.io.InputStream;
import java.util.List;

/**
 * 解析xml配置文件
 *
 * @author 召
 * @date 2022-04-09 21:42:08
 */
public class XmlBeanDefinitionReader implements BeanDefinitionReader {

    /**
     * 注册表
     */
    private BeanDefinitionRegistry registry;

    public XmlBeanDefinitionReader() {
        this.registry = new SimpleBeanDefinitionRegistry();
    }

    @Override
    public BeanDefinitionRegistry getRegistry() {
        return this.registry;
    }

    @Override
    public void loadBeanDefinitions(String configLocation) throws Exception {
        // 使用dom4j对xml文件进行解析
        SAXReader reader = new SAXReader();
        // 获取类路径下的配置文件
        InputStream is = this.getClass().getClassLoader().getResourceAsStream(File.separator + configLocation);
        Document document = reader.read(is);
        // 根据document对象获取根标签(beans)
        Element rootElement = document.getRootElement();
        // 根据根标签获取下面的所有bean标签
        List<Element> beanList = rootElement.elements("bean");
        for (Element bean : beanList) {
            // 获取id属性
            String id = bean.attributeValue("id");
            // 获取class属性
            String className = bean.attributeValue("class");
            // 创建BeanDefinition对象
            BeanDefinition beanDefinition = new BeanDefinition();
            beanDefinition.setId(id);
            beanDefinition.setClassName(className);
            // 获取所有property标签
            List<Element> propertyList = bean.elements("property");
            // 创建MutablePropertyValues对象
            MutablePropertyValues propertyValues = new MutablePropertyValues();
            for (Element property : propertyList) {
                // 获取name属性
                String name = property.attributeValue("name");
                // 获取ref属性
                String ref = property.attributeValue("ref");
                // 获取value属性
                String value = property.attributeValue("value");
                // 创建PropertyValue对象并初始化
                PropertyValue propertyValue = new PropertyValue(name, ref, value);
                // 将其添加到propertyValues中进行管理
                propertyValues.addPropertyValue(propertyValue);
            }
            beanDefinition.setPropertyValues(propertyValues);
            // 将BeanDefinition对象添加到注册表中
            this.registry.registerBeanDefinition(id, beanDefinition);
        }
    }
}
